//
//  KyBaseViewController.swift
//  KyBase
//
//  Created by fangyukui on 2017/10/16.
//  Copyright © 2017年 fangyukui. All rights reserved.
//

import UIKit

class KyBaseViewController: UIViewController {
    lazy var kyNavBar:UINavigationBar  =  UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: 69))
    lazy var kyNaVItem  = UINavigationItem()
    
    var tableView : UITableView?
    
    var isPullup : Bool = false
    
    var visitorInfo : Dictionary<String,Any>?
    
    
    lazy var refreshCtl : UIRefreshControl = {
        let refreshCtl =  UIRefreshControl()
        refreshCtl.addTarget(self, action: #selector(valueChanged), for: .valueChanged)
        return refreshCtl
    }()
    
    
    var visitorV : KyVisitorView?;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        loadData()
    }
    
    override var title: String?{
        didSet{
            kyNaVItem.title = title
        }
    }
    deinit {
        KyLog(message:"deinit -> \(type(of: self))")
        
    }

    

}

//MARK:设置UI
extension KyBaseViewController{
    fileprivate func setupUI() {
        view.backgroundColor = UIColor.randomColor
        //系统tabBar隐藏，会自动缩进20点 设置false取消缩进
        automaticallyAdjustsScrollViewInsets = false
        setupNav()
        KyNetworkManager.shared.isUserLogin ? setupTableView() : setupVisitorView()
        
        
        
        
    }
    func loadData() {
        refreshCtl.endRefreshing()
    }
    //自定义导航条
    fileprivate func setupNav(){
        kyNavBar.items = [kyNaVItem]
        kyNavBar.barTintColor = UIColor(hexColor: "F6F6F6")
        view.addSubview(kyNavBar)
    }
    
     func setupTableView(){
        tableView = UITableView(frame:CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.height) , style: .plain)
        tableView?.delegate = self
        tableView?.dataSource = self
        tableView?.separatorStyle = .none
        view.insertSubview(tableView!, belowSubview: kyNavBar)
        
        //设置内容缩进
        tableView?.contentInset = UIEdgeInsets(top: kyNavBar.yk_h, left: 0, bottom: kyNavBar.yk_h, right: 0)
        
        tableView?.refreshControl = refreshCtl
        
    }
    fileprivate func setupVisitorView(){
        visitorV = KyVisitorView(frame: view.bounds)
        visitorV?.visitorInfo = visitorInfo
        view.insertSubview(visitorV!, belowSubview: kyNavBar)
        visitorV?.registerBtn.addTarget(self, action: #selector(register), for: .touchUpInside)
        visitorV?.loginBtn.addTarget(self, action: #selector(login), for: .touchUpInside)
        kyNaVItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(register))
        kyNaVItem.rightBarButtonItem = UIBarButtonItem(title: "登陆", style: .plain, target: self, action: #selector(register))
        kyNavBar.tintColor = .orange
        
      
        
    }
    
}

//MARK:tableView协议
extension KyBaseViewController:UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 0.01
        
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let row = indexPath.row
        let section = indexPath.section
        if (section == tableView.numberOfSections-1) && (row == tableView.numberOfRows(inSection: section)-1) {
            
            //上拉刷新条件
            
            if isPullup == false{
               isPullup = true
               loadData()
            }
            
            
        }
        
        
    }
    
    
    
    
    
}
//MARK:事件
extension KyBaseViewController{
    @objc fileprivate func valueChanged(){
        loadData()
        
    }
    @objc fileprivate func register(){
        KyLog(message: "register")
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: KyUserLoginNotification), object: nil)
    }
    @objc fileprivate func login(){
        KyLog(message: "register")
    }
}




